package cn.itcast.tlias.com.tliaswork.service.impl;

import cn.itcast.tlias.com.tliaswork.mapper.EmpMapper;
import cn.itcast.tlias.com.tliaswork.pojo.Emp;
import cn.itcast.tlias.com.tliaswork.pojo.PageBean;
import cn.itcast.tlias.com.tliaswork.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired(required = false)
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize) {


//订单，发货单等都是翻页的
//        Long count = empMapper.searchCount();
//
//        //记录开始索引
//        Integer start = (page - 1) * pageSize;
//
//
//        List<Emp> listEmps = empMapper.searchLimit(start, pageSize);

//        读取数据端采用PageHelp

        PageHelper.startPage(page, pageSize);
        List<Emp> empList = empMapper.list();

        Page<Emp> p = (Page<Emp>) empList;

        PageBean bean = new PageBean(p.getTotal(),p.getResult());

        return bean;
    }

    /**
     * 更多查询条件查询
     * @param page 第几页
     * @param pageSize 多少条
     * @param name 姓名
     * @param gender 性别 1男 2女
     * @param begin //创建时间
     * @param end 修改时间
     * @return
     */
    @Override
    public PageBean pageCondition(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        //分页方式不变
        PageHelper.startPage(page, pageSize);
        List<Emp> empList = empMapper.listCondition(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {

        log.info("批量删除操作======{}",ids);
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp query(Integer id) {
       Emp emp= empMapper.query(id);
       return emp;
    }

    @Override
    public void updata(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updata(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return  empMapper.getByUsernameAndPassword(emp);
    }
}
